package com.tang.common.utils.excel;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.streaming.SXSSFSheet;

/**
 * excel标题与内容初始化样式设置
 */
public class ExcelStyleHandler extends HorizontalCellStyleStrategy implements SheetWriteHandler {

    /**
     * 设置表格头部居中显示策略
     */
    public ExcelStyleHandler() {
        super(ExcelCommon.writeCellStyles.get(0),ExcelCommon.writeCellStyles.get(1));
    }

    /**
     * 设置表格内容显示策略
     */
    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

        for (int i = 0; i < 100; i++) {
            // 设置为文本格式
            SXSSFSheet sxssfSheet = (SXSSFSheet) writeSheetHolder.getSheet();
            CellStyle cellStyle = writeWorkbookHolder.getCachedWorkbook().createCellStyle();
            // 49为文本格式
            cellStyle.setDataFormat((short) 49);
            // 右对齐
            cellStyle.setAlignment(HorizontalAlignment.RIGHT);
            // i为列，一整列设置为文本格式
            sxssfSheet.setDefaultColumnStyle(i, cellStyle);

        }
    }
}
